cmake_minimum_required(VERSION 3.5.1)
project(libav_nodelet)

# add_definitions(-std=c++11 -Wall -O2)
add_definitions(-std=c++11 -Wall -g)
option(WITH_ROS "Compile as ros package" OFF)
if (WITH_ROS)
  message(STATUS "Compiling as ros package")
  add_definitions(-DUSE_ROS)
  find_package(catkin REQUIRED COMPONENTS
    cv_bridge
    image_transport
    nodelet
    roscpp
    sensor_msgs
  )

  catkin_package(
    INCLUDE_DIRS include
  #  LIBRARIES libav_nodelet
    CATKIN_DEPENDS nodelet sensor_msgs cv_bridge image_transport
  #  DEPENDS system_lib
  )
endif()

find_package(OpenCV REQUIRED)
message("Using OpenCV: ${OpenCV_INCLUDE_DIRS}")

find_library(AVCODEC_LIBRARY avcodec)
find_library(AVFORMAT_LIBRARY avformat)
find_library(AVUTIL_LIBRARY avutil)
find_library(AVDEVICE_LIBRARY avdevice)
find_library(SWSCALE_LIBRARY swscale)
if (WITH_ROS)
  include_directories(
    include
    /usr/include/x86_64-linux-gnu
    ${catkin_INCLUDE_DIRS}
    ${OpenCV_INCLUDE_DIRS}
  )
else()
  include_directories(
    include
    /usr/include/x86_64-linux-gnu
    ${OpenCV_INCLUDE_DIRS}
  )
endif()

add_library(libav SHARED src/libav.cpp src/udp.cpp)
if (WITH_ROS)
  target_link_libraries(libav PUBLIC
    ${AVCODEC_LIBRARY}
    ${AVFORMAT_LIBRARY}
    ${AVUTIL_LIBRARY}
    ${AVDEVICE_LIBRARY}
    ${SWSCALE_LIBRARY}
    ${OpenCV_LIBS}
    ${catkin_LIBRARIES})
else()
  target_link_libraries(libav PUBLIC
    ${AVCODEC_LIBRARY}
    ${AVFORMAT_LIBRARY}
    ${AVUTIL_LIBRARY}
    ${AVDEVICE_LIBRARY}
    ${SWSCALE_LIBRARY}
    ${OpenCV_LIBS})
endif()

add_executable(test_libav test/test_libav.cpp)
target_link_libraries(test_libav libav)

if (WITH_ROS)
  # Receive video from stream and send to topic
  add_library(av_nodelet src/av_nodelet.cpp)
  target_link_libraries(av_nodelet libav ${catkin_LIBRARIES})

  # ============ TEST & EXAMPLE ============
  # Nodelet version of receiving image via topic
  add_library(test_nodelet test/test_nodelet.cpp)
  target_link_libraries(test_nodelet ${catkin_LIBRARIES} ${OpenCV_LIBS})

  # Node version of receiving image via topic
  add_library(test_node test/test_node.cpp)
  target_link_libraries(test_node ${catkin_LIBRARIES} ${OpenCV_LIBS})
endif()
